package com.mindcoord.handbook.service.impl;

import com.mindcoord.handbook.dao.StudentMapper;
import com.mindcoord.handbook.common.Result;
import com.mindcoord.handbook.pojo.Student;
import com.mindcoord.handbook.service.StudentService;

public class StudentServiceImpl implements StudentService {

    private StudentMapper studentMapper;

    public void setStudentMapper(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    @Override
    public Result listStudents() {
        Result result;
        try {
            result = Result.success(studentMapper.listStudents());
        } catch (Exception e) {
            result = Result.fail("查询失败");
        }
        return result;
    }

    @Override
    public Result listStudentsByClazzId(Integer clazzId) {
        Result result;
        try {
            result = Result.success(studentMapper.listStudentsByClazzId(clazzId));
        } catch (Exception e) {
            result = Result.fail("查询失败");
        }
        return result;
    }

    @Override
    public Result getStudentById(Integer id) {
        Result result;
        try {
            result = Result.success(studentMapper.getStudentById(id));
        } catch (Exception e) {
            result = Result.fail("查询失败");
        }
        return result;
    }

    @Override
    public Result insertStudent(Student student) {
        Result result;
        try {
            Boolean b = studentMapper.insertStudent(student);
            result = b ? Result.success(null) : Result.fail("插入失败");
        } catch (Exception e) {
            result = Result.fail("插入失败");
        }
        return result;
    }

    @Override
    public Result deleteStudentById(Integer id) {
        Result result;
        try {
            Boolean b = studentMapper.deleteStudentById(id);
            result = b ? Result.success(null) : Result.fail("删除失败");
        } catch (Exception e) {
            result = Result.fail("删除失败");
        }
        return result;
    }

    @Override
    public Result updateStudent(Student student) {
        Result result;
        try {
            Boolean b = studentMapper.updateStudent(student);
            result = b ? Result.success(null) : Result.fail("更新失败");
        } catch (Exception e) {
            result = Result.fail("更新失败");
        }
        return result;
    }
}
